/**
 * 
 */
package kr.pe.johnkim.dayve.provider;

import java.util.ArrayList;

import kr.pe.johnkim.dayve.activity.R;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * @author johnkim
 *
 */
public class DayveFavoriteAdapter extends BaseAdapter {
	
	public static final String TAG = DayveFavoriteAdapter.class.getSimpleName();
	
	private Context context;
	private ArrayList<DayveCategory> data;
	private DayveCategory item;
	LayoutInflater inf;
	
	View view;
	
	public DayveFavoriteAdapter(Context context, ArrayList<DayveCategory> data) {
		this.context = context;
		this.data = data;
		inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		view = convertView;
		
		ViewWrapper wrapper = null;
		
		if(view == null) {
			view = inf.inflate(R.layout.dayve_favorite_row, parent, false);
			wrapper = new ViewWrapper(view);
			view.setTag(wrapper);
		} else {
			wrapper = (ViewWrapper)view.getTag();
		}
		
		item = data.get(position);
		
//		Log.i(TAG, "favoriteYn  : " + item.getFavoriteYn());
//		Log.i(TAG, "name        : " + item.getName());
//		Log.i(TAG, "items count : " + item.getItemsCount());

		// 카테고리명을 세팅 한다 : 카테고리명 앞뒤로 약간의 공백을 둔다.
		wrapper.getTxtViewFavoriteCategoryName().setText("  " + item.getName() + "  ");
		// 카테고리에 등록된 아이템의 총 갯수를 세팅 한다.
		wrapper.getTxtViewFavoriteCategoryItemCount().setText(item.getItemsCount());
		
		return view;
	}

	/**
	 * View Wrapper class.
	 * 
	 * @author johnkim
	 *
	 */
	class ViewWrapper {
		View base;
		
		TextView txtViewFavoriteCategoryName;					// 카테고리명.
		TextView txtViewFavoriteCategoryItemCount;				// 카테고리에 등록된 아이템 총 갯수.
//		TextView txtViewCategoryItemRegisterDate;		// 카테고리에 가장 최근에 등록된 아이템의 등록 일자.
		
		ViewWrapper(View base) {
			this.base = base;
		}

		TextView getTxtViewFavoriteCategoryName() {
			if(txtViewFavoriteCategoryName == null) {
				txtViewFavoriteCategoryName = (TextView)view.findViewById(R.id.txtViewFavoriteCategoryName);
			}
			
			return (txtViewFavoriteCategoryName);
		}
		
		TextView getTxtViewFavoriteCategoryItemCount() {
			if(txtViewFavoriteCategoryItemCount == null) {
				txtViewFavoriteCategoryItemCount = (TextView)view.findViewById(R.id.txtViewFavoriteCategoryItemCount);
			}
			
			return (txtViewFavoriteCategoryItemCount);
		}
		
//		TextView getTxtViewCategoryItemRegisterDate() {
//			if(txtViewCategoryItemRegisterDate == null) {
//				txtViewCategoryItemRegisterDate = (TextView)view.findViewById(R.id.txtViewCategoryItemRegisterDate);
//			}
//			
//			return (txtViewCategoryItemRegisterDate);
//		}
	}
}
